<?php

/*
 * litchicms
 * Copyright (C) 2010-7-2 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/**
 * 创建表
 */
function DB_create_table($tablename, $fieldarr) {
    global $Db;
    $arr = array();
    $arr[0] = FALSE;
    $sql = "SHOW  TABLES";
    $arr[1] = array();
    $arr[1][] = $sql;
    $rs = brk_query($sql);
    while ($row = $Db->fetch_array($rs)) {
        if ($tablename == current($row)) {
            $arr[2] = "数据表已经存在";
            return $arr;
        }
    }

    $PRIMARY_KEY = "";
    $KEY = array();

    $sql = "CREATE TABLE `$tablename` (";
    foreach ($fieldarr as $key => $value) {
        $temp = "";
        $temp.=" `$key` " . $value['type'];
        $temp.=empty($value['length']) ? " " : " (" . $value['length'] . ") ";
        $temp.= ! empty($value['not null']) ? " NOT NULL " : " NULL ";
        $temp.=empty($value['default']) ? " " : " default " . $value['default'] . " ";
        $temp.=empty($value['auto_increment']) ? " " : " auto_increment ";

        $temp.=",";

        $sql.=$temp;

        if (!empty($value['PRIMARY KEY'])) {
            $PRIMARY_KEY = "PRIMARY KEY  (`$key`)";
        }

        if (!empty($value['KEY'])) {
            $KEY[] = ", KEY `$key` (`$key`) ";
        }
    }

    $sql.=$PRIMARY_KEY;

    $sql.=implode(" ", $KEY);

    $sql.=")";

    $sql.="ENGINE=MyISAM  DEFAULT CHARSET=utf8";


    $arr[1][] = $sql;

    if (!brk_query($sql)) {
        $arr[2] = "创建数据表失败";
        return $arr;
    }
    $arr[0] = TRUE;
    return $arr;
}

/**
 * 删除字段
 */
function DB_column_del($tablename, $columnname) {
    global $Db;

    $arr = array();
    $arr[0] = FALSE;
    $arr[1] = array();

    $arr[1][] = $sql = "SHOW COLUMNS FROM $tablename";

    $fields = array();
    $rs = brk_query($sql);
    while ($row = $Db->fetch_array($rs)) {
        $fields[] = $row['Field'];
    }
    foreach ($columnname as $value) {
        if (!in_array($value, $fields)) {
            $arr[2] = "无法删除 字段{$value} ,因为他不存在";
            return $arr;
        }
        $arr[1][] = $sql = "ALTER TABLE $tablename DROP COLUMN $value";
        if (!brk_query($sql)) {
            $arr[2] = "删除字段{$value}失败";
            return $arr;
        }
    }

    $arr[0] = TRUE;
    return $arr;
}

/**
 * 添加字段
 */
function DB_column_add($tablename, $columns) {
    global $Db;

    $arr = array();
    $arr[0] = FALSE;
    $arr[1] = array();

    $arr[1][] = $sql = "SHOW COLUMNS FROM $tablename";

    $fields = array();
    $rs = brk_query($sql);
    while ($row = $Db->fetch_array($rs)) {
        $fields[] = $row['Field'];
    }
    foreach ($columns as $value) {
        if (in_array($value, $fields)) {
            $arr[2] = "无法添加 字段{$value} ,因为他已存在";
            return $arr;
        }
    }

    $columns_format = array();
    foreach ($columns as $key => $value) {
        $temp = " `$key` " . $value['type'];
        $temp.=empty($value['length']) ? " " : " (" . $value['length'] . ") ";
        $temp.= ! empty($value['not null']) ? " NOT NULL " : " NULL ";
        $temp.=empty($value['default']) ? " " : " default " . $value['default'] . " ";
        $temp.=empty($value['auto_increment']) ? " " : " auto_increment ";

        $columns_format[] = $temp;

        if (!empty($value['PRIMARY KEY'])) {
            $columns_format[] = "PRIMARY KEY  (`$key`)";
        }

        if (!empty($value['KEY'])) {
            $columns_format[] = ", KEY `$key` (`$key`) "; //KEY `group_id` (`group_id`)
        }
    }

    $arr[1][] = $sql = "ALTER TABLE $tablename ADD (" . implode(',', $columns_format) . ")";

    if (!brk_query($sql)) {
        $arr[2] = "添加字段失败";
        return $arr;
    }

    $arr[0] = TRUE;
    return $arr;
}

/**
 * 删除表
 */
function DB_del_table($tablename) {
    global $Db;
    $arr = array();
    $arr[0] = FALSE;
    $arr[1] = array();
    $sql = "DROP TABLE IF EXISTS `$tablename`";
    if (!brk_query($sql)) {
        $arr[2] = "删除数据表失败";
        return $arr;
    }

    $arr[0] = TRUE;
    return $arr;
}

/*
 * 计算数据库大小
 */

function DB_get_size() {
    $tables = DB_table_status();
    if (!$tables) {
        return "查寻表信息失败";
    }
    $i = 0;
    foreach ($tables as $value) {
        $i+=$value['Data_length'];
        $i+=$value['Index_length'];
    }
    return sprintf("%01.2f", $i/1024/1024);
}

/*
 * 查出所有表
 */

function DB_table_status() {
    global $Db;
    $rs = brk_query("SHOW TABLE STATUS");
    if (!$rs) {
        return FALSE;
    }
    $all_tables = array();
    while ($row = mysql_fetch_assoc($rs)) {
        $all_tables[] = $row;
    }
    return $all_tables;
}